home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1681 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.7 KB

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why use private class members instead of protected?
  5. Date: Fri, 12 Jan 1996 09:32:44 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <30F61CAC.2781E494@intellektik.informatik.th-darmstadt.de>
  8. References: <30F4AB49.6ABB@sierra.net> <30F50874.15FB7483@intellektik.informatik.th-darmstadt.de> <4d3q07$n8o@geraldo.cc.utexas.edu>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Richard Kilgore wrote:
  16. > Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> wrote:
  17. > >TGColwell wrote:
  18. > >>
  19. > >> I'm relatively new to c++.  I have one quick question:  If child
  20. > >> classes can only access protected members of the parent class,
  21. > >> why make any members of any class private?  Wouldn't it be
  22. > >> better to make members of the parent class protected so that the
  23. > >> class is alway "inheritance ready"?
  24. > >>
  25. > >
  26. > >No,
  27. > >it's quite often a better approach to make these data-members private and
  28. > >provide the suitable accessor functions. In this way you can change the
  29. > >representation of the data-members without the problem that modifications
  30. > >will 'propagate' downwards the class-hierachie.
  31. > >
  32. > >       Enno
  33. > I'd like to add my 2 cents worth here.  I have on occasion seen the approach
  34. > of making a data member private, and then providing an accessor/mutator
  35. > combination method like the following:
  36. >         DataType& MyClass::data() { return _data; }
  37. > The idea is to provide one function that the user can call to get the data's
  38. > value OR to change it.
  39. > This makes no sense to me.  You have just set yourself up for a pain in the
  40. > ass if you ever want to change the internal representation of the data.
  41. > For example, suppose you store a fraction as two integers and provide
  42. > accessor/mutators like the line above for each of the numerator and
  43. > denominator values.  Then later you want to represent it as a float and
  44. > calculate such values on the fly.  Don't know if this is a good exmaple
  45. > of a reasonable thing to do, but you get the picture.  Now you're stuck with
  46. > an interface that promises you can return a REFERENCE to an integer value.
  47. > But how do you return a reference to something that doesn't exist in memory?
  48. > One idea is to keep a temporary storage location around for returning such
  49. > values: a serious kludge in which the user better not hold onto his own
  50. > reference to your return value, because it might change soon.  Another
  51. > approach entails allocating memory for the return type and expecting the user
  52. > to free it: Yuck!  Inneficient and dangerous.  In either case, a user's
  53. > attempt to use this returned value as an Lvalue will have no effect on the
  54. > class's data.
  55. > I realize you might want to return a reference for efficiency's sake, if the
  56. > data type being returned is large, but there is no reason not to return a
  57. > CONST reference for this purpose.  Then if you ever change the internal
  58. > implementation of the class, you could use the temporary storage location
  59. > for return values and not have to worry about cooky side effects.
  60.  
  61. You're right. If you return a reference to a private data-member changing the
  62. representation becomes complicated, if not impossible.
  63. However an accessor/mutator pair like
  64.     
  65.         void MyClass :: set_data(const DataType&) { ... }
  66.         DataType MyClass :: get_data() const { return _data; }
  67.  
  68. completely avoids this sort of problems. If the 'DataType' uses some technique
  69. to speed up copying (for example ref-counting) the overhead is minimal.
  70.  
  71.     Enno
  72.